<?php
/**
 * Created by PhpStorm.
 * User: HS032
 * Date: 2020/7/16
 * Time: 15:13
 */

namespace pay\libs;


use pay\modules\frontend\models\Application;
use yii\base\Exception;

class CheckSign extends JudgeSign
{


    /*** 20200729 支付校验参数
     * @param $data
     * @return array
     */
    public function checkSignWxPay($data)
    {
        if (!isset($data['signType']) || empty($data['signType'])){
            throw new Exception('签名有误，signType',10044);
        }
        $paramsData = '';

        if ($data['signType'] == "MD5"){
            $this->signPay($data);
            $this->getMd5Data($data);  // 校验Md5参数
            $paramsData = $data;
        }elseif($data['signType'] == 'AES'){
            $paramsData = $this->getAesData($data);// 解密Aes参数
            if (!empty($paramsData)){
               $this->signPay($paramsData);
            }else{
                throw new Exception('签名错误-解密失败',40055);
            }
        }
        return $paramsData;

    }

    /*** 校验退款参数
     * @param $data
     * @return array
     */
    public function checkSignRefund($data)
    {
		
        if (!isset($data['signType']) || empty($data['signType'])){
            throw new Exception('签名有误，signType',10044);
        }
        $paramsData = '';
        if ($data['signType'] == 'MD5'){  // 签名返回时Md5

            $this->signRefund($data);
            $this->getMd5Data($data);  // 校验Md5参数
            $paramsData = $data;

        }elseif($data['signType'] == 'AES'){   // 签名方式为AES
            $paramsData = $this->getAesData($data);// 解密Aes参数
            if (!empty($paramsData)){
                $this->signRefund($paramsData);
            }else{
                throw new Exception('签名数据有误',40055);
            }
        }
        return $paramsData;

    }


    /*** 校验查询订单的参数
     * @param $data
     * @return array
     */
    public function checkSignViewOrder($data)
    {
        if (!isset($data['signType']) || empty($data['signType'])){
            throw new Exception('签名有误，signType',10044);
        }
        $paramsData = '';
        if ($data['signType'] == 'MD5'){  // 签名返回时Md5
            $this->signViewOrder($data);
            $this->getMd5Data($data);  // 校验Md5参数
            $paramsData = $data;
        }elseif($data['signType'] == 'AES'){   // 签名方式为AES
            $paramsData = $this->getAesData($data);  // 解密Aes参数
            if (!empty($paramsData)){
                $this->signViewOrder($paramsData);
            }else{
                throw new Exception('签名有误',40055);
            }
        }
        return $paramsData;
    }


    /*** 支付宝支付 参数校验
     * @param $data
     * @return string
     * @throws Exception
     */
    public  function checkSignAliPay($data)
    {

        if (!isset($data['signType']) || empty($data['signType'])){
            throw new Exception('签名有误，signType',10044);
        }
        $paramsData = '';
        if ($data['signType'] == "MD5"){
            $this->signAliPay($data);
            $this->getMd5Data($data);  // 校验Md5参数
            $paramsData = $data;
        }elseif($data['signType'] == 'AES'){
            $paramsData = $this->getAesData($data);// 解密Aes参数
            if (!empty($paramsData)){
                $this->signAliPay($paramsData);
            }else{
                throw new Exception('签名有误，signType',40055);

            }
        }
        return $paramsData;

    }


    /**** 支付宝退款 参数校验
     * @param $data
     * @return array|string
     * @throws Exception
     */
    public function checkSignAliRefund($data)
    {

        if (!isset($data['signType']) || empty($data['signType'])){
            throw new Exception('签名有误，signType',10044);
        }
        $paramsData = '';

        if ($data['signType'] == "MD5"){
            $this->signAliRefund($data);
            $this->getMd5Data($data);  // 校验Md5参数
            $paramsData = $data;
        }elseif($data['signType'] == 'AES'){
            $paramsData = $this->getAesData($data);// 解密Aes参数
            if (!empty($paramsData)){
                $this->signAliRefund($paramsData);
            }else{
                throw new Exception('签名有误，解密，signType',40055);

            }
        }
        return $paramsData;
    }



    /*** 验证时间戳与随机字符串， 时间戳必须是小于3分钟的，随机字符串要保证请求时间100内保持唯一；
     * @param $time
     * @param $str
     * @return array
     */
    private function checkTimeStr($time,$str,$type = 'pay')
    {
//        if (time() > (int)$time + 300){
//            return ['code' => 99,'msg' => '支付时间戳过期'];
//        }
//        $cache = \Yii::$app->cache;
//        $status = $cache->get($type.$str);
//        if ($status){
//            return ['code' => 99,'msg' => '随机字符串已使用过'];
//        }else{
//            $cache->set($type.$str,'1',100);
//        }
        return ['code' => 0,'msg' => 'ok'];

    }

    /*** 解密Aes参数
     * @param $data
     * @return array
     */
    private function getAesData($data)
    {
        $this->setAppLicat($data['aid']);
        $appLicat = $this->appLicat;  // 获取应用key
        if (empty($appLicat)){
            throw new Exception('签名错误-aid有误',40055);
        }
        $key = $appLicat['aes_key'];
        $iv = $appLicat['aes_iv'];
        $paramsData = json_decode($this->decryption($data['data'],$key,$iv),true);
        return $paramsData;
    }


    /*** 校验Md5参数
     * @param $data
     * @return array
     */
    private function getMd5Data($data)
    {
        $appLicat = $this->appLicat;  // 获取应用key
        if (empty($appLicat)){
            throw new Exception('签名错误',40055);
        }
        $sign = empty($data['sign'])?'':$data['sign'];
        $postSign = Tools::computeSign($data,$appLicat['a_key']);
//            var_dump($postSign);die;
        if ($sign !== $postSign){
            throw new Exception('签名错误',10011);
        }

        return true;
    }





}